/*
 * 
 * Created on Jul 27, 2005
 */
package remato.client.view.requirementstable;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.TableItem;

import remato.client.model.AbstractPropertySpesification;
import remato.client.model.ClientModelItem;
import remato.client.model.facade.IPropertiesContainer;
import remato.client.view.facade.IModelTableEditor;

/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: TableCellModifier.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public class TableCellModifier implements ICellModifier {

    private IModelTableEditor<ClientModelItem> tableEditor;
    private List<AbstractPropertySpesification> spesifications;
    private String[] columnNames;
    /**
     * 
     */
    public TableCellModifier(List<AbstractPropertySpesification> spesifications, String[] columnNames, IModelTableEditor<ClientModelItem> tableEditor) {
        super();
        this.spesifications = spesifications;
        this.columnNames = columnNames;
        this.tableEditor = tableEditor;
    }

    /**
     * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
     */
    public boolean canModify(@SuppressWarnings("unused") Object element, @SuppressWarnings("unused") String property) {
        //TODO check if Role is good
        return true;
    }

    /**
     * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
     */
    public Object getValue(Object element, String property) {
//        System.out.println(getClass().getName() + ".getValue(...): element type: " + element.getClass().getName());
        if (element instanceof ClientModelItem) {
    
            // Find the index of the column
            int index = Arrays.asList(columnNames).indexOf(property);
    
            AbstractPropertySpesification propertyId = spesifications.get(index);
    
            return ((IPropertiesContainer) element).getCurrentPropertyValue(propertyId).getCellEditorValue();

        }
        return null;
    }

    /**
     * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
     */
    public void modify(Object element, String property, Object newValue) {
//        System.out.println(getClass().getName() + ".getValue(...): element type: " + element.getClass().getName());
//        System.out.println(getClass().getName() + ".setValue(...): value type: " + value.getClass().getName());

        Object object = ((TableItem)element).getData(); 
        if (object instanceof ClientModelItem) {
            // Find the index of the column 
            int index = Arrays.asList(columnNames).indexOf(property);
    
            AbstractPropertySpesification propertyId = spesifications.get(index);
                
            ClientModelItem item = (ClientModelItem) object;
            
//            PropertyValue propertyValue = item.getCurrentPropertyValue(propertyId);
            
            if (item.setPropertyValueFromCellEditor(item.getCurrentPropertyValue(propertyId).getPropertySpesification(), newValue)) {
                tableEditor.updateElement(item);
            }

//            ( item).getCurrentPropertyValue(propertyId).setValueFromCellEditor(value);


//            TableItem item = (TableItem) element;
//    
//            AbstractPropertySpesification spec = (AbstractPropertySpesification) item.getData();
//    
//            UserTypesTableColumnsDefinition.values()[columnIndex].setColumnValueFromTableRow(spec, value);
//            
//            tableEditor.updateElement(spec);
        }
    }

}
/**
 * 
 * Changelog:
 * 
 * $Log: TableCellModifier.java,v $
 * Revision 1.1  2006-01-23 19:58:37  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.4  2006-01-03 13:33:02  petterei
 * adjustments and finery
 *
 * Revision 1.3  2005-12-05 04:16:40  petterei
 * *** empty log message ***
 *
 * Revision 1.2  2005/10/24 16:25:36  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/10/11 15:06:59  petterei
 * Seccond iteration:
 * 1. Eclipse based client with new domain model
 * 2. Flexible Project and Category hierarchy with Drag-n-Drop functionality
 * New functionality:
 * 3. Eclipse plugin update-site
 *
 * Revision 1.4  2005/08/26 15:11:45  peide3
 * fixed bug in properties view (using propertyspecification as id, instead of object)
 *
 * Revision 1.3  2005/08/25 18:14:26  peide3
 * updated services interface
 *
 * Revision 1.2  2005/08/25 09:46:30  peide3
 * added batch update + bugfixes
 *
 * Revision 1.1  2005/08/23 06:38:13  peide3
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/31 22:55:10  petterei
 * initial commit
 *
 */
